package xj.graph2d.command;

import xj.graph2d.Command;
import xj.graph2d.GraphDocument;
import xj.graph2d.GraphEdgeBase;
import xj.graph2d.GraphNodeBase;

public class EdgeAuxAction implements Command.AuxAction {

  public static final int EDGE_CONNECTION = 0x01;

  public static final int EDGE_DISCONNECTION = 0x02;

  public static final int EDGE_SOURCE = 0x10;

  public static final int EDGE_DESTINATION = 0x20;

  public EdgeAuxAction(GraphDocument graph, 
		       GraphEdgeBase edge, 
		       GraphNodeBase node,
		       int end, int action) {
    this.graph = graph;
    this.edge = edge;
    this.node = node;
    this.end = end;
    this.action = action;
  }

  public void forward() {
    if (graph != null && edge != null && node != null) {
      if (action == EDGE_CONNECTION) {
	if (end == EDGE_SOURCE) {
	  graph.connectSource(edge, node);
	} else {
	  graph.connectDestination(edge, node);
	}
      } else {
	if (end == EDGE_SOURCE) {
	  graph.disconnectSource(edge);
	} else {
	  graph.disconnectDestination(edge);
	}
      }
    }
  }

  public void backward() {
    if (graph != null && edge != null && node != null) {
      if (action == EDGE_CONNECTION) {
	if (end == EDGE_SOURCE) {
	  graph.disconnectSource(edge);
	} else {
	  graph.disconnectDestination(edge);
	}
      } else {
	if (end == EDGE_SOURCE) {
	  graph.connectSource(edge, node);
	} else {
	  graph.connectDestination(edge, node);
	}
      }
    }
  }

  public boolean isAlwaysAfter() {
    return false;
  }

  @Override
  public String toString() {
    return ("EdgeAuxAction: " + (end == EDGE_SOURCE ? "src" : "des") + 
	    " " + (action == EDGE_CONNECTION ? "connect" : "disconnect"));
  }

  GraphDocument graph;

  GraphEdgeBase edge;

  GraphNodeBase node;

  int end;

  int action;

}
